<template>
	<view>
		<snowy-input :fieldConfig="fieldConfig" v-model="localFieldData" />
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['radio','checkbox',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<uni-data-checkbox :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" v-model="localFieldData" :multiple="fieldConfig.options.isMultiple" :map="fieldConfig.options.map" :localdata="fieldConfig.options.options" />
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['select',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<snowy-sel-picker :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" v-model="localFieldData" :isMultiple="fieldConfig.options.isMultiple" :map="fieldConfig.options.map" @getOptData="(param, callback) => callback({ state: CallbackState.SUCCESS, data: fieldConfig.options.options })" />
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['date'].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<uni-datetime-picker :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" v-model="props.fieldData" :type="getDatetimePickerType(fieldConfig.options.range, fieldConfig.options.showTime)" :placeholder="fieldConfig.options.placeholder" :start-placeholder="fieldConfig.options.rangePlaceholder?fieldConfig.options.rangePlaceholder[0]:null" :end-placeholder="fieldConfig.options.rangePlaceholder?fieldConfig.options.rangePlaceholder[1]:null" return-type="string" :clear-icon="fieldConfig.options.clearable">
			</uni-datetime-picker>
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['time',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<view style="flex: 1;">
				<!-- start="09:01" end="21:01" -->
				<picker :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" mode="time" :value="localFieldData" @change="(e)=>{localFieldData = e.detail.value}">
					<view class="uni-input input-value-border">{{localFieldData?localFieldData:fieldConfig.options.placeholder}}</view>
				</picker>
			</view>
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['rate',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<uni-rate :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" v-model="localFieldData" />
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['slider',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<slider :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" :min="fieldConfig.options.min" :max="fieldConfig.options.max" :value="localFieldData" :step="fieldConfig.options.step" @change="(e)=>{localFieldData = e.detail.value}" show-value />
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['switch',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<switch :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" :checked="localFieldData" @change="(e)=>{localFieldData = e.detail.value}" />
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['uploadFile',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<snowy-file-picker-api :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" idDataType="urlArr" v-model="localFieldData">
			</snowy-file-picker-api>
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['uploadImg',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<snowy-file-picker-api :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" idDataType="urlArr" v-model="localFieldData">
			</snowy-file-picker-api>
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['userSelector',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<snowy-user-picker :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" v-model="localFieldData" :isMultiple="fieldConfig.options.isMultiple" :placeholder="fieldConfig.options.placeholder" :map="{key: 'id', label: 'name'}" @getOptData="selectorFunction.user.getOptData" :isPage="true" @getSelData="selectorFunction.user.getSelData">
			</snowy-user-picker>
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['orgSelector',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<snowy-sel-picker :searchEnabled="true" :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" v-model="localFieldData" :isMultiple="fieldConfig.options.isMultiple" :placeholder="fieldConfig.options.placeholder" :map="{key: 'id', label: 'name'}" @getOptData="selectorFunction.org.getOptData" :isPage="true" @getSelData="selectorFunction.org.getSelData">
			</snowy-sel-picker>
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['posSelector',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<snowy-sel-picker :searchEnabled="true" :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" v-model="localFieldData" :isMultiple="fieldConfig.options.isMultiple" :placeholder="fieldConfig.options.placeholder" :map="{key: 'id', label: 'name'}" @getOptData="selectorFunction.pos.getOptData" :isPage="true" @getSelData="selectorFunction.pos.getSelData">
			</snowy-sel-picker>
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['roleSelector',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<snowy-sel-picker :searchEnabled="true" :disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.options.disabled) === 'boolean' ? fieldConfig.options.disabled : false" v-model="localFieldData" :isMultiple="fieldConfig.options.isMultiple" :placeholder="fieldConfig.options.placeholder" :map="{key: 'id', label: 'name'}" @getOptData="selectorFunction.role.getOptData" :isPage="true" @getSelData="selectorFunction.role.getSelData">
			</snowy-sel-picker>
		</uni-forms-item>
		<uni-forms-item v-if="fieldConfig && fieldConfig.type && ['editor',].includes(fieldConfig.type) && (typeof(fieldConfig.options.hidden) === 'boolean' ? !fieldConfig.options.hidden : true)" :label="fieldConfig.label" :name="fieldConfig.name" :required="fieldConfig.required" :rules="fieldConfig.rules">
			<editor :placeholder="fieldConfig.options.placeholder"></editor>
		</uni-forms-item>
		<snowy-batch-form ref="snowyBatchFormRef" :fieldConfig="fieldConfig" v-model="localFieldData" />
	</view>
</template>
<script>
	export default {
		name: 'SnowyFormItem'
	}
</script>
<script setup>
	import snowyInput from './snowy-input.vue'
	import snowyBatchForm from './snowy-batch-form.vue'
	import { nextTick, reactive, ref, watch, getCurrentInstance } from "vue"
	import processApi from '@/api/flw/process-api'
	import userCenterApi from '@/api/sys/user-center-api'
	import taskApi from '@/api/flw/task-api'
	import CallbackState from "@/enum/callback-state"
	const { proxy } = getCurrentInstance()
	const emits = defineEmits(['update:fieldData', 'fieldDataChange'])
	const props = defineProps({
		fieldConfig: {
			type: Object,
			required: true
		},
		fieldConfigDisable: {
			type: Boolean,
			default: null,
			required: false
		},
		fieldData: {
			required: true
		},
		useDefault: {
			type: Boolean,
			default: false,
			required: false
		}
	})
	// 父子组件-双向数据绑定
	const localFieldData = ref()
	watch(() => props.fieldData, (newValue, oldValue) => {
		if (JSON.stringify(newValue) !== JSON.stringify(oldValue)) {
			localFieldData.value = JSON.parse(JSON.stringify(newValue))
		}
	}, {
		deep: true,
		immediate: true
	})
	watch(() => localFieldData.value, (newValue, oldValue) => {
		if (JSON.stringify(newValue) !== JSON.stringify(props.fieldData)) {
			emits('update:fieldData', JSON.parse(JSON.stringify(newValue)))
			emits('fieldDataChange', JSON.parse(JSON.stringify(newValue)))
		}
	}, {
		deep: true,
		immediate: false
	})
	if (props.useDefault) {
		// 默认值处理
		if (['input', 'textarea', 'number', ].includes(props.fieldConfig.type)) {
			localFieldData.value = props.fieldConfig.options.defaultValue
		} else if (['radio', 'checkbox', ].includes(props.fieldConfig.type)) {
			localFieldData.value = props.fieldConfig.options.defaultValue
		} else if (['select', ].includes(props.fieldConfig.type)) {
			if (props.fieldConfig.options.isMultiple) {
				localFieldData.value = [props.fieldConfig.options.defaultValue]
			} else {
				localFieldData.value = props.fieldConfig.options.defaultValue
			}
		} else if (['date', ].includes(props.fieldConfig.type)) {
			if (props.fieldConfig.options.range) {
				localFieldData.value = props.fieldConfig.options.rangeDefaultValue
			} else {
				localFieldData.value = props.fieldConfig.options.defaultValue
			}
		} else if (['time', ].includes(props.fieldConfig.type)) {
			localFieldData.value = props.fieldConfig.options.defaultValue
		} else if (['rate', ].includes(props.fieldConfig.type)) {
			localFieldData.value = props.fieldConfig.options.defaultValue
		} else if (['slider', ].includes(props.fieldConfig.type)) {
			localFieldData.value = props.fieldConfig.options.defaultValue
		} else if (['switch', ].includes(props.fieldConfig.type)) {
			localFieldData.value = props.fieldConfig.options.defaultValue
		}
	}
	// 获取日期时间字段的类型
	const getDatetimePickerType = (range, showTime) => {
		if (range) {
			if (showTime) {
				return 'datetimerange'
			} else {
				return 'daterange'
			}
		} else {
			if (showTime) {
				return 'datetime'
			} else {
				return 'date'
			}
		}
	}
	// 传递用户选择器需要的API
	const selectorFunction = {
		// 用户
		user: {
			getOptData: async ({ pageNo, pageSize, keyword }, callback) => {
				const parameter = { current: pageNo, size: pageSize, searchKey: keyword }
				const data = await taskApi.taskUserSelector(parameter)
				callback({ state: CallbackState.SUCCESS, data: data?.records })
			},
			getSelData: async (curSelDataKey, callback) => {
				if (uni.$snowy.tool.isNotEmpty(curSelDataKey)) {
					if (!props.fieldConfig.options.isMultiple) {
						const data = await userCenterApi.userCenterGetUserListByIdList({
							idList: [curSelDataKey]
						})
						callback({ state: CallbackState.SUCCESS, data: data[0] })
						return
					}
					if (!!props.fieldConfig.options.isMultiple) {
						const data = await userCenterApi.userCenterGetUserListByIdList({
							idList: curSelDataKey
						})
						callback({ state: CallbackState.SUCCESS, data: data })
						return
					}
				}
			}
		},
		// 组织
		org: {
			getOptData: async ({ pageNo, pageSize, keyword }, callback) => {
				const parameter = { current: pageNo, size: pageSize, searchKey: keyword }
				const data = await taskApi.taskOrgSelector(parameter)
				callback({ state: CallbackState.SUCCESS, data: data?.records })
			},
			getSelData: async (curSelDataKey, callback) => {
				if (uni.$snowy.tool.isNotEmpty(curSelDataKey)) {
					if (!props.fieldConfig.options.isMultiple) {
						const data = await userCenterApi.userCenterGetOrgListByIdList({
							idList: [curSelDataKey]
						})
						callback({ state: CallbackState.SUCCESS, data: data[0] })
						return
					}
					if (!!props.fieldConfig.options.isMultiple) {
						const data = await userCenterApi.userCenterGetOrgListByIdList({
							idList: curSelDataKey
						})
						callback({ state: CallbackState.SUCCESS, data: data })
						return
					}
				}
			}
		},
		// 职位
		pos: {
			getOptData: async ({ pageNo, pageSize, keyword }, callback) => {
				const parameter = { current: pageNo, size: pageSize, searchKey: keyword }
				const data = await taskApi.taskPositionSelector(parameter)
				callback({ state: CallbackState.SUCCESS, data: data?.records })
			},
			getSelData: async (curSelDataKey, callback) => {
				if (uni.$snowy.tool.isNotEmpty(curSelDataKey)) {
					if (!props.fieldConfig.options.isMultiple) {
						const data = await userCenterApi.userCenterGetPositionListByIdList({
							idList: [curSelDataKey]
						})
						callback({ state: CallbackState.SUCCESS, data: data[0] })
						return
					}
					if (!!props.fieldConfig.options.isMultiple) {
						const data = await userCenterApi.userCenterGetPositionListByIdList({
							idList: curSelDataKey
						})
						callback({ state: CallbackState.SUCCESS, data: data })
						return
					}
				}
			}
		},
		// 角色
		role: {
			getOptData: async ({ pageNo, pageSize, keyword }, callback) => {
				const parameter = { current: pageNo, size: pageSize, searchKey: keyword }
				const data = await taskApi.taskRoleSelector(parameter)
				callback({ state: CallbackState.SUCCESS, data: data?.records })
			},
			getSelData: async (curSelDataKey, callback) => {
				if (uni.$snowy.tool.isNotEmpty(curSelDataKey)) {
					if (!props.fieldConfig.options.isMultiple) {
						const data = await userCenterApi.userCenterGetRoleListByIdList({
							idList: [curSelDataKey]
						})
						callback({ state: CallbackState.SUCCESS, data: data[0] })
						return
					}
					if (!!props.fieldConfig.options.isMultiple) {
						const data = await userCenterApi.userCenterGetRoleListByIdList({
							idList: curSelDataKey
						})
						callback({ state: CallbackState.SUCCESS, data: data })
						return
					}
				}
			}
		},
	}
	const snowyBatchFormRef = ref()
	// 表单校验
	const formListValidate = () => {
		if (props.fieldConfig.type === 'batch') {
			return new Promise((resolve, reject) => {
				snowyBatchFormRef.value.submit().then((result) => {
					resolve(result)
				}).catch(error => {
					reject(error)
				})
			})
			// const promiseList = []
			// if (localFieldData.value) {
			// 	localFieldData.value.forEach((item, index) => {
			// 		promiseList.push(new Promise((resolve, reject) => {
			// 			proxy.$refs[`batch-form-ref-${ props.fieldConfig.name }-${ index }`][0].validate().then(result => {
			// 				resolve(result)
			// 			}).catch(err => {
			// 				reject(err)
			// 			})
			// 		}))
			// 	})
			// }
			// return new Promise((resolve, reject) => {
			// 	Promise.all(promiseList).then((result) => {
			// 		resolve(result)
			// 	}).catch(err => {
			// 		reject(err)
			// 	})
			// })
		}
	}
	defineExpose({
		formListValidate
	})
</script>
<style lang="scss">
	.uni-input {
		height: 50rpx;
		padding: 15rpx 25rpx;
		line-height: 50rpx;
		font-size: 28rpx;
		background: #FFF;
		flex: 1;
	}

	.input-value-border {
		border: 1px solid $uni-border-2;
		border-radius: 5upx;
	}
</style>